﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 尺寸标注
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUDimensionRef 的包装</remarks>
    public class Dimension : DrawingElement
    {
        #region API 函数
        /// <summary>
        /// 获取标注类型
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionGetType(IntPtr dimension, out SUDimensionType type);

        /// <summary>
        /// 获取文字
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="text"><see cref="StringRef"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionGetText(IntPtr dimension, ref IntPtr text);

        /// <summary>
        /// 设置文字
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionSetText(IntPtr dimension, [MarshalAs(UnmanagedType.LPUTF8Str)] string text);

        /// <summary>
        /// 获取标注所在平面
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="plane"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionGetPlane(IntPtr dimension, ref Plane3D plane);

        /// <summary>
        /// 判断是否是3D标注文字
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="is_text_3d"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionGetText3D(IntPtr dimension, out bool is_text_3d);

        /// <summary>
        /// 设置是否是3D文字
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="is_text_3d"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionSetText3D(IntPtr dimension, bool is_text_3d);

        /// <summary>
        /// 获取箭头类型
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionGetArrowType(IntPtr dimension, out ArrowType type);

        /// <summary>
        /// 设置箭头类型
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionSetArrowType(IntPtr dimension, ArrowType type);

        /// <summary>
        /// 获取字体
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="font"><see cref="Font"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionGetFont(IntPtr dimension, out IntPtr font);

        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="font"><see cref="Font"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionSetFont(IntPtr dimension, IntPtr font);
        #endregion

        /// <summary>
        /// 标注文字字体
        /// </summary>
        public Font Font
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionGetFont(Id, out IntPtr font));
                return new Font(font);
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionSetFont(Id, value.Id));
            }
        }

        /// <summary>
        /// 标注箭头类型
        /// </summary>
        public ArrowType ArrowType
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionGetArrowType(Id, out ArrowType type));
                return type;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionSetArrowType(Id, value));
            }
        }

        /// <summary>
        /// 标注类型
        /// </summary>
        public SUDimensionType DimType
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionGetType(Id, out SUDimensionType type));
                return type;
            }
        }

        /// <summary>
        /// 标注文字
        /// </summary>
        public string Text
        {
            get
            {
                StringRef s = StringRef.Empty;
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionGetText(Id, ref s.Id));
                return s.Dispose();
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionSetText(Id, value));
            }
        }

        /// <summary>
        /// 是否是3D文字
        /// </summary>
        public bool Is3DText
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionGetText3D(Id, out bool is3d));
                return is3d;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionSetText3D(Id, value));
            }
        }

        /// <summary>
        /// 标注所在平面
        /// </summary>
        public Plane3D Plane
        {
            get
            {
                Plane3D p = new Plane3D();
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionGetPlane(Id, ref p));
                return p;
            }
        }

        internal Dimension(IntPtr id) : base(id) { }
    }
}
